<template>
  <div class="goodsinfo-containter">
    <transition @before-enter="beforeEnter"
                @enter="enter"
                @after-enter="afterEnter">
      <div class="ball"
           v-show="ballFlag"
           ref="ball"></div>
    </transition>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :bannerList="img"></swiper>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：<del>￥{{goodsinfo.old_price}}</del>&nbsp;&nbsp;销售价：<span class="now_price">￥{{goodsinfo.new_price}}</span>
          </p>
          <p>
            购买数量：<numbox @getCount="getSelectedCount"
                    :max="goodsinfo.stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary"
                       size="small">立即购买</mt-button>
            <mt-button type="danger"
                       size="small"
                       @click="addToShopCart">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsinfo.goods_no}}</p>
          <p>库存情况：{{goodsinfo.stock_quantity}}</p>
          <p>上架时间：{{goodsinfo.add_time}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary"
                   size="large"
                   plain
                   @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger"
                   size="large"
                   plain
                   @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from '../subcomponents/swiper.vue';
import numbox from '../subcomponents/goodsinfo_numbox.vue';

export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [],
      img: [],
      goodsinfo: {},
      ballFlag: false,
      selectedCount: 1
    }
  },
  created() {
    this.getlunbotu();
    this.getgoodsinfo();
  },
  methods: {
    getlunbotu() {
      this.axios.get('/getthumimages/' + this.id).then(res => {
        res.data.img_url.forEach(el => {
          this.img.push(el.src);
        });
        this.lunbotu = res.data;
      })
    },
    getgoodsinfo() {
      this.axios.get('/getgoodsinfo/' + this.id).then(res => {
        this.goodsinfo = res.data;
      })
    },
    goDesc(id) {
      this.$router.push({ name: 'goodsdesc', params: { id } });
    },
    goComment(id) {
      this.$router.push({ name: 'goodscomment', params: { id } });
    },
    addToShopCart() {
      this.ballFlag = !this.ballFlag;
      let goodsinfo = { id: this.id, count: this.selectedCount, price: this.goodsinfo.new_price, selected: true };
      this.$store.commit('addToCart', goodsinfo);
    },
    beforeEnter(el) {
      el.style.transform = 'translate(0, 0)';
    },
    enter(el, done) {
      let ballPosition = this.$refs.ball.getBoundingClientRect();
      let badgePosition = document.querySelector('#badge').getBoundingClientRect();
      let xDist = badgePosition.left - ballPosition.left;
      let yDist = badgePosition.top - ballPosition.top;
      el.offsetTop;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = '0.5s cubic-bezier(0.4,-0.3,1,0.68)';
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      this.selectedCount = count;
    }

  },
  components: { swiper, numbox }
}
</script>

<style lang="scss" scoped>
.goodsinfo-containter {
  background-color: #eee;
  overflow: hidden;
  .now_price {
    font-size: 16px;
    font-weight: bold;
    color: red;
  }
  .mui-card-footer {
    flex-wrap: wrap;
    button {
      margin: 10px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    background-color: red;
    border-radius: 50%;
    position: absolute;
    z-index: 50;
    top: 390px;
    left: 145px;
  }
}
</style>